package level.editor.control.tile;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowFocusListener;
import java.io.File;
import java.io.FilenameFilter;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;

import level.editor.control.Control;
import level.editor.control.IconConstants;
import level.editor.control.main.MainTabbed;
import level.editor.view.Window;
import level.editor.view.action.ActionConstants;

public class TileControl extends Control implements MouseListener
{

	public TileControl(MainTabbed tabbed)
	{
		super(tabbed);
		addMouseListener(this);
		init();
	}

	public void createTileSelect(FileSelect fileSelect)
	{
		((TileHandler) handler).createTileSelect(fileSelect.getDirString()
				+ "\\" + (String) fileSelect.getSelectedItem());
	}

	private void init()
	{
		setName("Tiles: No file loaded");
		setIcon(new ImageIcon(ActionConstants.ICON_DIR + "tile.png"));
		setMinimumSize(new Dimension(320, 100));
		initFileSelect();
		initTileSelect();
	}

	@Override
	public void mouseClicked(MouseEvent arg0)
	{
		requestFocusInWindow();
	}

	@Override
	public void mouseEntered(MouseEvent arg0)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent arg0)
	{
		requestFocusInWindow();
	}

	@Override
	public void mouseReleased(MouseEvent arg0)
	{
		requestFocusInWindow();
	}

	private void initFileSelect()
	{
		FileSelect fileSelect;
		JPanel panel = new JPanel();
		panel.add(new JLabel("Select file: "));
		panel.add(fileSelect = new FileSelect());
		panel.add(new DirectorySelect(fileSelect));
		add(panel, BorderLayout.SOUTH);
	}

	private void initTileSelect()
	{
		handler = new TileHandler(this);
		((Window) tabbed.getTopLevelAncestor())
				.addWindowFocusListener((WindowFocusListener) handler);

	}

	private class FileSelect extends JComboBox<String> implements
			ActionListener
	{

		private File dir;

		public FileSelect()
		{
			super();
			setFocusable(false);
			setDir(TileConstants.TILE_DIR);
			addActionListener(this);
		}

		public void setDir(File dir)
		{
			this.dir = dir;
			setActionCommand("Inactive");
			this.removeAllItems();
			addItems();
			setSelectedIndex(-1);
			setActionCommand("Active");
		}

		public String getDirString()
		{
//			String file = dir.getParentFile().getParent();
//			System.out.println(dir.getAbsolutePath());
			return dir.getPath();
		}

		@Override
		public void actionPerformed(ActionEvent e)
		{
			if (e.getSource() == this && e.getActionCommand().equals("Active"))
			{
				if (getSelectedItem() != null)
				{
					TileControl.this.setName("Tiles: "
							+ (String) getSelectedItem());
					TileControl.this.createTileSelect(this);
				}
				revalidate();
			}

			if (e.getSource() instanceof DirectorySelect)
			{
				changeDirectory();
			}
		}

		private void addItems()
		{
			String[] toAdd = fileList();
			if (toAdd != null)
			{
				for (int i = 0; i < toAdd.length; i++)
				{
					this.addItem(toAdd[i]);
				}
			}
		}

		private String[] fileList()
		{
			FilenameFilter filter = new FilenameFilter()
			{
				public boolean accept(File dir, String name)
				{
					return name.endsWith(".png");
				}
			};
			String[] fileNames = dir.list(filter);
			if (fileNames == null)
			{
				System.out.println("No tiles in Directory");
				return null;
			}
			else
			{
				return fileNames;
			}
		}

		private void changeDirectory()
		{
			JFileChooser fc = new JFileChooser(getDirString());
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			int returnVal = fc.showOpenDialog(getWindow());

			if (returnVal != JFileChooser.APPROVE_OPTION)
			{
				return;
			}

			else if (returnVal == JFileChooser.APPROVE_OPTION)
			{
				setDir(fc.getSelectedFile());
			}
		}
	}

	private class DirectorySelect extends JButton
	{
		public DirectorySelect(FileSelect fileSelect)
		{
			super(new ImageIcon(IconConstants.ICON_DIR + "/open.png"));
			setFocusable(false);
			setPreferredSize(new Dimension(23, 23));
			addActionListener(fileSelect);
		}
	}
}
